// 意见反馈页面逻辑
Page({
  data: {
    selectedType: 'bug',
    content: '',
    contact: '',
    images: [],
    deviceInfo: {
      system: '',
      wechatVersion: '',
      appVersion: '1.0.0'
    },
    canSubmit: false,
    feedbackHistory: []
  },

  onLoad: function (options) {
    this.loadDeviceInfo();
    this.loadFeedbackHistory();
    this.checkCanSubmit();
  },

  // 加载设备信息
  loadDeviceInfo: function () {
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      'deviceInfo.system': systemInfo.system,
      'deviceInfo.wechatVersion': systemInfo.version
    });
  },

  // 加载反馈历史
  loadFeedbackHistory: function () {
    const history = wx.getStorageSync('feedbackHistory') || [];
    this.setData({ feedbackHistory: history });
  },

  // 选择反馈类型
  selectType: function (e) {
    const type = e.currentTarget.dataset.type;
    this.setData({ selectedType: type });
    this.checkCanSubmit();
  },

  // 输入反馈内容
  onContentInput: function (e) {
    this.setData({ content: e.detail.value });
    this.checkCanSubmit();
  },

  // 输入联系方式
  onContactInput: function (e) {
    this.setData({ contact: e.detail.value });
  },

  // 选择图片
  chooseImage: function () {
    wx.chooseImage({
      count: 3 - this.data.images.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const newImages = [...this.data.images, ...res.tempFilePaths];
        this.setData({ images: newImages });
      }
    });
  },

  // 删除图片
  deleteImage: function (e) {
    const index = e.currentTarget.dataset.index;
    const images = this.data.images;
    images.splice(index, 1);
    this.setData({ images: images });
  },

  // 检查是否可以提交
  checkCanSubmit: function () {
    const canSubmit = this.data.selectedType && this.data.content.trim().length > 0;
    this.setData({ canSubmit: canSubmit });
  },

  // 提交反馈
  submitFeedback: function () {
    if (!this.data.canSubmit) {
      return;
    }

    const typeMap = {
      'bug': '问题反馈',
      'feature': '功能建议',
      'content': '内容纠错',
      'other': '其他'
    };

    const feedback = {
      id: Date.now(),
      type: this.data.selectedType,
      typeText: typeMap[this.data.selectedType],
      content: this.data.content,
      contact: this.data.contact,
      images: this.data.images,
      deviceInfo: this.data.deviceInfo,
      time: this.formatTime(new Date()),
      status: 'pending',
      statusText: '待处理'
    };

    // 保存到本地
    const history = [feedback, ...this.data.feedbackHistory];
    wx.setStorageSync('feedbackHistory', history);
    this.setData({ feedbackHistory: history });

    // 模拟提交到服务器
    wx.showLoading({ title: '提交中...' });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });

      // 清空表单
      this.setData({
        selectedType: 'bug',
        content: '',
        contact: '',
        images: []
      });
      this.checkCanSubmit();

      // 返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }, 2000);
  },

  // 格式化时间
  formatTime: function (date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  // 分享功能
  onShareAppMessage: function () {
    return {
      title: '意见反馈 - 编程题库',
      path: '/pages/feedback/feedback'
    };
  }
}); 